package com.jerry.wschat.admin.security;

import com.jerry.wschat.model.Admin;
import com.jerry.wschat.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author Jerry.Wu
 * @description:
 * @date 2018/9/12 19:36
 */
@Component
public class MyAdminDetailsService implements UserDetailsService {

    @Autowired
    private AdminService mAdminService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = mAdminService.findAdminByUsername(username);
        if (admin == null) {
            throw new UsernameNotFoundException("用户名或密码错误！");
        }
        MyAdminDetails myAdminDetails = new MyAdminDetails(admin.getId(), admin.getUsername(), admin.getPassword(), admin.getNickname(), admin.getEnabled());
        return myAdminDetails;
    }

}
